<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.searchList"
    >
    </page-filter>
    <dialog-table
      ref="resourceTable"
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :row-key="rowKey"
      :checked-row-list="checkedRowList"
      :reserve-selection="true"
      :check-box="checkBox"
      @handleEvent="handleEvent"
    >
    </dialog-table>
  </page-dialog>
</template>
<script setup name="selectReceive">
import { getDeptAll } from '@/api/core'
import { getPendStoreReceivePage } from '@/api/asset'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {}
    },
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
})

const sortString = ref(undefined)
// 过滤相关配置
const filterInfo = ref({
  query: {},
  searchList: [
    {
      type: 'input',
      label: '单号',
      value: 'orderNoLike',
      event: search,
    },
    {
      label: '部门',
      value: 'deptId',
      type: 'slip-select',
      api: getDeptAll,
      query: { deptAuth: 'id', status: 1 },
      clearable: true,
      filterable: true,
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
  searchBtn: [],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: getPendStoreReceivePage,
  fieldList: [
    { label: '单号', value: 'orderNo' },
    { label: '部门', value: 'deptName' },
    { label: '供应商', value: 'supplierName' },
    { label: '状态', value: 'statusValue' },
    { label: '备注', value: 'remark' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择收货单',
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref([])

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件showDialog
  },
})

onMounted(() => {
  filterInfo.query = {
    ...filterInfo.query,
    ...props.query,
  }
  filterInfo.copyQuery = proxy.$fn.deepClone(filterInfo.query)
  proxy.$nextTick(() => {
    getList()
  })
})

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped lang="scss"></style>
